﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using X3DXNA.interfaces;
using X3DXNA.X3DSchema;
using X3DXNA.utils;
using X3DXNA.basicModels;
using X3DXNA.debug;
using X3DXNA.core.CustomNodes;

namespace X3DXNA.core.Controllers
{
    class AppearanceController
    {
        List<IUsable> appearances;
        Debug debugSystem;
        MaterialController materialController;

        public AppearanceController(Debug debugSystem)
        {
            appearances = new List<IUsable>();
            this.debugSystem = debugSystem;
            materialController = new MaterialController(debugSystem);
        }

        public MyAppearance handleAppearance(Core core, Appearance a)
        {
            if (a != null && a.USE == null)
            {
                MyAppearance newAppearance = new MyAppearance();
                newAppearance.DEF = a.DEF;

                //Salviamo le informazioni nella struttura MyMaterial.
                foreach (INode c in a.Childs)
                {
                    if (c is Material)
                    {
                        Material m = ((Material)c);
                        newAppearance.setAppearance = materialController.handleMaterial(m);
                        
                        if (core.checkRoutedObject(c))
                            core.addRoutedObject(newAppearance.getAppearance);
                    }
                    else
                    {
                        newAppearance.setAppearance = new MyMaterial();
                        #warning Aggiungere Texture e altri Appearance.
                    }
                }

                appearances.Add(newAppearance);

                return newAppearance;
            }
            else if (a.USE != null)
            {
                MyAppearance appearance = (MyAppearance)utility.findUsable(appearances, a.USE);
                if (appearance != null)
                    return appearance;
                else
                {
                    debugSystem.addMessage("Attenzione, utilizzo di USE improprio: Non è stata trovato alcuna Appearance definita con il tag DEF = " + a.USE + ". Verrà utilizzata un'appearance standard.", typeof(AppearanceController));
                    return new MyAppearance();
                }
            }
            else
                return new MyAppearance();
        }
    }
}
